home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / CALib & You… / Source / CASample / CAS_Menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-07  |  10.3 KB  |  520 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        CAS_Menu.c
  3.  
  4.     Contains:    Menu handling routines. 
  5.  
  6.     Written by:    David H Nelson
  7.  
  8.     Copyright © 1993-1995 ComponentWorks, All rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <9>     5/19/95    SJF        Fix the about box to use CARequestModalFocus
  13.          <5>     4/23/95    RB        OD Menu overhaul
  14.          <4>     1/23/95    RB        Added support for the "View In Window" menu item.
  15.          <3>     2/15/95    RB        Added support for the "Insert…" menu item
  16.          <2>     1/23/95    SJF        Added support for CALib.
  17.          <1>     11/20/93    DHN        Created.
  18. */
  19.  
  20. #ifdef USE_CALIB
  21. #include "CALib.h"
  22. #include "CAS_CAUtil.h"
  23. #endif
  24.  
  25. #include "CAS_Globals.h"
  26.  
  27. #include "CAS_Menu.h"
  28. #include "CAS_MenuItems.h"
  29. #include "CAS_Misc.h"
  30. #include "CAS_App.h"
  31. #include "CAS_Doc.h"
  32. #include "CAS_Event.h"
  33. #include "CAS_Win.h"
  34.  
  35.  
  36. //----------------------------------------------------------------------
  37. //     local prototypes
  38.  
  39. #if defined(__cplusplus)
  40. extern "C"
  41. {
  42. #endif
  43.  
  44. static Boolean Menu_LoadMenubar(
  45.     short        mbarResID,
  46.     Boolean        drawIt );
  47. static pascal Boolean aboutFilter(
  48.     DialogPtr    theDialog,
  49.     EventRecord    *theEvent,
  50.     short        *itemHit );
  51. static void Menu_DoAboutBox( void );
  52. static void Menu_HandleAppleMenu(
  53.     short        menuItem,
  54.     WindowPtr    theWindow );
  55. static void Menu_HandleFileMenu(
  56.     short        menuItem,
  57.     WindowPtr    theWindow );
  58. static void Menu_HandleEditMenu(
  59.     short        menuItem,
  60.     WindowPtr    theWindow );
  61.  
  62. #ifdef USE_CALIB
  63. static void Menu_HandleCALibMenu(
  64.     short        menuItem,
  65.     WindowPtr    theWindow );
  66. #endif
  67.  
  68.  
  69. #if defined(__cplusplus)
  70. }
  71. #endif
  72.  
  73.  
  74. //---------------------------------------------------------------------------
  75.  
  76. void Menu_SetUpMenus( void )
  77. {
  78. MenuHandle    targetMenuH;
  79. short        i;
  80.  
  81. /* $$$$$ need proper OD menus (Document/Edit) here */
  82. /* File-Quit == Document-Close */
  83.  
  84.     // If a MBAR rsrc kMenubar_standard doesn't exist, build it manually
  85.  
  86.     if (!Menu_LoadMenubar( kMenubar_standard, false ))
  87.     {
  88.         for (i=kAppleMenu; i<=kToolMenu; i++)
  89.         {
  90.             targetMenuH = GetMenu( i );
  91.             if (targetMenuH != nil)
  92.             {
  93.                 InsertMenu( targetMenuH, 0 );
  94.                 if (i == kAppleMenu)
  95.                     AppendResMenu( targetMenuH, 'DRVR' );
  96.             }
  97.  
  98.         }
  99.     }
  100.  
  101. // $$$$$ don’t do this, because it inserts the Tools menu for the 2nd time!!!
  102. //    App_SetUpMenus();
  103.  
  104. #ifdef USE_CALIB
  105.     if (gCALibExists)
  106.     {
  107.         OSErr        theErr;
  108.  
  109.         // Share the base menu configuration with CALib/OpenDoc
  110.         CASetBaseMenuBar( GetMenuBar() );
  111.         if (theErr = CAError())
  112.             ;        // handle the error
  113.  
  114.         // Now add in the tool menu to the tool bar, it was removed from the MBAR
  115.         // resource in the OD resource files
  116.     
  117.         App_SetUpMenus();
  118.     
  119.         CARegisterCommand( kCACommandAppleMenu,        kAppleMenu, 0 );
  120.         CARegisterCommand( kCACommandAbout,            kAppleMenu, kAboutItem );
  121.     
  122.         CARegisterCommand( kCACommandOpen,            kFileMenu, kOpenSelectionItem );
  123.         CARegisterCommand( kCACommandClose,            kFileMenu, kCloseItem );
  124.  
  125.         CARegisterCommand( kCACommandInsert,        kFileMenu, kInsertItem );
  126.     
  127.         CARegisterCommand( kCACommandUndo,            kEditMenu, kUndoItem);
  128.         CARegisterCommand( kCACommandRedo,            kEditMenu, kRedoItem);
  129.         CARegisterCommand( kCACommandCut,            kEditMenu, kCutItem);
  130.         CARegisterCommand( kCACommandCopy,            kEditMenu, kCopyItem);
  131.         CARegisterCommand( kCACommandPaste,            kEditMenu, kPasteItem);
  132.         CARegisterCommand( kCACommandPasteAs,        kEditMenu, kPasteAsItem);
  133.         CARegisterCommand( kCACommandClear,            kEditMenu, kClearItem);
  134.         CARegisterCommand( kCACommandSelectAll,        kEditMenu, kSelectAllItem);
  135.         CARegisterCommand( kCACommandGetPartInfo,    kEditMenu, kPartInfoItem);
  136.         CARegisterCommand( kCACommandPreferences,    kEditMenu, kPreferencesItem);
  137.         CARegisterCommand( kCACommandViewAsWin,        kEditMenu, kViewInWindowItem);
  138.     }
  139. #endif
  140.  
  141.     DrawMenuBar();
  142.  
  143.     gAppMenuBar = GetMenuBar();
  144.  
  145. }
  146.  
  147. //---------------------------------------------------------------------------
  148. // load and optionally redraw a new menu bar
  149.  
  150. static Boolean Menu_LoadMenubar(
  151.     short        mbarResID,
  152.     Boolean        drawIt )
  153. {
  154. Handle        menuBarHandle    = NULL;
  155. MenuHandle    menuHandle        = NULL;
  156. Boolean        didIt            = false;
  157.  
  158.     menuBarHandle = GetNewMBar( mbarResID );
  159.  
  160.     if (menuBarHandle)
  161.     {
  162.         SetMenuBar( menuBarHandle );
  163.         menuHandle = GetMenuHandle( kAppleMenu );
  164.         if (menuHandle != NULL)
  165.             AppendResMenu( menuHandle, 'DRVR' );
  166.         didIt = true;
  167.     }
  168.  
  169.     if (drawIt && (menuBarHandle != NULL))
  170.         DrawMenuBar();
  171.  
  172.     return didIt;
  173. }
  174.  
  175.  
  176. //---------------------------------------------------------------------------
  177. // filter for about box. handles update events. returns true for mouseDown or
  178. // keyDown and false for all other events.
  179.  
  180. static pascal Boolean aboutFilter(
  181.     DialogPtr        theDialog,
  182.     EventRecord        *theEvent,
  183.     short            *itemHit )
  184. {    
  185.     switch (theEvent->what)
  186.     {
  187.         case updateEvt:
  188.             // try to handle the update; assume we did not handle it.
  189.             Event_HandleUpdateEvent( theEvent );
  190.             break;
  191.  
  192.         case activateEvt:
  193.             // try to handle the update; assume we did not handle it.
  194.             Event_HandleActivateEvent( theEvent );
  195.             break;
  196.  
  197.         case mouseDown:
  198.         case keyDown:
  199.             // a click or a keystroke should release the dialog
  200.             *itemHit = 1;
  201.             return true;        // we handled the event.
  202.  
  203.         default:
  204.             // for any other event, fall through.
  205.             break;
  206.     }
  207.  
  208.     // we filter out all other events.
  209.     return false;
  210. }
  211.  
  212. //---------------------------------------------------------------------------
  213. // Display the about box
  214. static void Menu_DoAboutBox( void )
  215. {
  216. GrafPtr            savedPort;
  217. DialogPtr        theDialog;
  218. short            iItemHit;
  219. ModalFilterUPP    aboutFilterUPP;
  220. WindowPtr        frontWindow;
  221.  
  222.     GetPort( &savedPort );
  223.     App_ForceActivateFrontWindow( false );
  224.  
  225. #ifdef USE_CALIB
  226.     if (gCALibExists)
  227.     {
  228.         frontWindow = CAGetFrontDocWindow();
  229.         if (!CARequestModalFocus( frontWindow))
  230.         {
  231. //            DisposeDialog( theDialog );
  232.             return;
  233.         }
  234.     }
  235. #endif
  236.     theDialog = GetNewDialog( kAboutBoxID, 0L, nil /* INFRONT*/ );
  237.  
  238.     if (theDialog == nil)
  239.         return;
  240.  
  241.  
  242.     SetPort( (GrafPtr)theDialog );
  243.     SelectWindow( (WindowPtr)theDialog );
  244.  
  245.     aboutFilterUPP = NewModalFilterProc( aboutFilter );
  246.  
  247.     do
  248.     {
  249.         ModalDialog( aboutFilterUPP, &iItemHit );
  250.     }
  251.     while (iItemHit != 1);
  252.  
  253. #ifdef USE_CALIB
  254.     if (gCALibExists)
  255.         CARelinquishModalFocus( frontWindow );
  256. #endif
  257.  
  258.     DisposeRoutineDescriptor( aboutFilterUPP );
  259.     DisposeDialog( theDialog );
  260.  
  261.     App_ForceActivateFrontWindow( true );
  262.     SetPort( savedPort );
  263. }
  264.  
  265. //---------------------------------------------------------------------------
  266. // Menu_AdjustMenus - 
  267.  
  268. void Menu_AdjustMenus(
  269.     WindowPtr    theWindow )
  270. {
  271. MenuHandle    fileMenuH;
  272. Boolean        isAppWindow;
  273.  
  274.     fileMenuH = GetMenuHandle( kFileMenu );
  275.     if (fileMenuH != nil)
  276.     {
  277.         isAppWindow = Win_IsAppWindow( theWindow );
  278.  
  279.         // adjust the close, save, saveAs, page setup, and print menu items.
  280.         mhSetMenuItem(
  281.             isAppWindow || Win_IsDAWindow( theWindow ),
  282.             fileMenuH, kCloseItem );
  283.         mhSetMenuItem( isAppWindow, fileMenuH, kSaveItem );
  284.         mhSetMenuItem( isAppWindow, fileMenuH, kSaveAsItem );
  285.         mhSetMenuItem( isAppWindow, fileMenuH, kPageSetUpItem );
  286.         mhSetMenuItem( isAppWindow, fileMenuH, kPrintItem );
  287.     }
  288.  
  289.     // give the app a chance to adjust any menus.
  290.     App_AdjustMenus( theWindow );
  291.  
  292. }
  293.  
  294.  
  295. //---------------------------------------------------------------------------
  296.  
  297. void Menu_HandleMenu(
  298.     long        mSelect,
  299.     WindowPtr    theWindow )
  300. {
  301. short    menuID, menuItem;
  302.  
  303.     menuID = HiWord( mSelect );
  304.     menuItem = LoWord( mSelect );
  305.  
  306. #ifdef USE_CALIB
  307.     if (gCALibExists)
  308.     {
  309.         // CADispatchMenuEvent manufactures an event record if one isn't provided
  310.         if (CADispatchMenuEvent( NULL, mSelect ))
  311.         {
  312.             return;
  313.         }
  314.  
  315.     }
  316. #endif
  317.  
  318.     switch (menuID)
  319.     {
  320.         case kAppleMenu:
  321.             Menu_HandleAppleMenu( menuItem, theWindow );
  322.             break;
  323.  
  324.         case kFileMenu:
  325.             Menu_HandleFileMenu( menuItem, theWindow );
  326.             break;
  327.  
  328.         case kEditMenu:
  329.             Menu_HandleEditMenu( menuItem, theWindow );
  330.             break;
  331.  
  332.  
  333.         default:
  334.             App_HandleMenu( mSelect, theWindow );
  335.             break;
  336.     }
  337.  
  338.     HiliteMenu( 0 );
  339.  
  340. }
  341.  
  342. //---------------------------------------------------------------------------
  343. // Handle the Apple menu items here.
  344.  
  345. static void Menu_HandleAppleMenu(
  346.     short        menuItem,
  347.     WindowPtr    theWindow )
  348. {
  349. Str255        name;
  350.  
  351.     switch (menuItem)
  352.     {
  353.         case kAboutItem:        // is it the about item.
  354.             Menu_DoAboutBox();
  355.             break;
  356.  
  357.         default:                // It must be a DA
  358.             GetMenuItemText( GetMenuHandle( kAppleMenu ), menuItem, name );
  359.             OpenDeskAcc( name );
  360.             break;
  361.     }
  362. }
  363.  
  364. //---------------------------------------------------------------------------
  365. // Handle the standard File menu items here.
  366.  
  367. static void Menu_HandleFileMenu(
  368.     short        menuItem,
  369.     WindowPtr    theWindow )
  370. {
  371.     switch (menuItem)
  372.     {
  373.         case kNewItem:
  374.             App_NewMenu();
  375.             break;
  376.  
  377.         case kOpenItem:
  378.             App_OpenMenu();
  379.             break;
  380.  
  381.         case kCloseItem:
  382.             App_CloseMenu( theWindow );
  383.             break;
  384.  
  385.         case kSaveItem:
  386.             App_SaveMenu( theWindow );
  387.             break;
  388.  
  389.         case kSaveAsItem:
  390.             App_SaveAsMenu( theWindow );
  391.             break;
  392.  
  393.         case kPageSetUpItem:
  394.             App_PageSetUpMenu( theWindow );
  395.             break;
  396.  
  397.         case kPrintItem:
  398.             App_PrintMenu( theWindow );
  399.             break;
  400.  
  401.         case kQuitItem:
  402.             App_QuitMenu();
  403.             break;
  404.  
  405. #ifdef USE_CALIB
  406.         case kInsertItem:
  407.             CAUtil_InsertMenu( theWindow );
  408.             break;
  409.         
  410.         case kOpenSelectionItem:
  411.             CAUtil_OpenSelectionMenu( theWindow );
  412.             break;
  413. #endif
  414.  
  415.         default:
  416.             App_HandleFileMenu( menuItem, theWindow );
  417.             break;
  418.     }
  419. }
  420.  
  421. //---------------------------------------------------------------------------
  422. // Handle standard Edit menu items here.
  423.  
  424. static void Menu_HandleEditMenu(
  425.     short        menuItem,
  426.     WindowPtr    theWindow )
  427. {
  428. DocPtr        theDoc;
  429.  
  430.     if (Win_IsAppWindow( theWindow ))
  431.     {
  432.         // get the theDoc
  433.         theDoc = (DocPtr)GetWRefCon( theWindow );
  434.  
  435.         switch (menuItem)
  436.         {
  437.             case kUndoItem:
  438.                 //Doc_UndoMenu( theDoc );
  439.                 break;
  440.  
  441.             case kCutItem:
  442.                 Doc_CutMenu( theDoc );
  443.                 break;
  444.  
  445.             case kCopyItem:
  446.                 Doc_CopyMenu( theDoc, false );
  447.                 break;
  448.  
  449.             case kPasteItem:
  450.                 Doc_PasteMenu( theDoc );
  451.                 break;
  452.  
  453.             case kClearItem:
  454.                 Doc_ClearMenu( theDoc );
  455.                 break;
  456.  
  457.             case kSelectAllItem:
  458.                 Doc_SelectAllMenu( theDoc );
  459.                 break;
  460.  
  461. #ifdef USE_CALIB
  462.             case kPartInfoItem:
  463.                 CAUtil_PartInfoMenu( theDoc );
  464.                 break;
  465. #endif
  466.             default:
  467.                 App_HandleEditMenu( menuItem, theWindow );
  468.                 break;
  469.         }
  470.     }
  471.     else    // it's not a document window
  472.     {
  473.         switch (menuItem)
  474.         {
  475.             case kUndoItem:
  476.             case kCutItem:
  477.             case kCopyItem:
  478.             case kPasteItem:
  479.                 SystemEdit( menuItem - 1 );
  480.                 break;
  481.  
  482.             default:
  483.                 break;
  484.         }
  485.     }
  486. }
  487.  
  488.  
  489. //---------------------------------------------------------------------------
  490. #ifdef USE_CALIB
  491.  
  492. static void Menu_HandleCALibMenu(
  493.     short        menuItem,
  494.     WindowPtr    theWindow )
  495.  
  496. {
  497. DocPtr        theDoc;
  498.  
  499.     if (Win_IsAppWindow( theWindow ))
  500.     {
  501.         // get the theDoc
  502.         theDoc = (DocPtr)GetWRefCon( theWindow );
  503.  
  504.         switch (menuItem)
  505.         {
  506.  
  507.             case kSelectAllParts:
  508.  
  509.                 break;
  510.  
  511.             default: break;
  512.  
  513.         }
  514.  
  515.     }
  516.  
  517.  
  518. }
  519. #endif
  520.